//
//  NSString+PYRegularFormat.m
//  catuncel
//
//  Created by Jim on 2017/12/3.
//  Copyright © 2017年 Jim. All rights reserved.
//

#import "NSString+PYRegularFormat.h"

#import "NSDate+JZCalendarLogic.h"

@implementation NSString (PYRegularFormat)

- (NSString *)regularFormat
{
    if (self) {
        
        NSString *time;

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

        NSDate *date = [dateFormatter dateFromString:self];

        NSTimeInterval timeInterval = [[date localDate] timeIntervalSince1970];

        NSDate *currentDate = [[NSDate date] localDate];

        NSTimeInterval currentTimeInterval = [currentDate timeIntervalSince1970];

        NSTimeInterval difference = currentTimeInterval - timeInterval;

        NSArray *dateArray = [self componentsSeparatedByString:@" "];

        NSArray *currentDateArray = [[NSString stringWithFormat:@"%@", currentDate] componentsSeparatedByString:@" "];

        NSInteger day = 0;

        NSInteger currentDay = 0;

        NSInteger month = 0;

        NSInteger currentMonth = 0;

        NSInteger year = 0;

        NSInteger currentYear = 0;

        if (dateArray.count == 2) {
            
            day = [[[[dateArray firstObject] componentsSeparatedByString:@"-"] lastObject] integerValue];
            
            currentDay = [[[[currentDateArray firstObject] componentsSeparatedByString:@"-"] lastObject] integerValue];
            
            month = [[[[dateArray firstObject] componentsSeparatedByString:@"-"] objectAtIndex:1] integerValue];
            
            currentMonth = [[[[currentDateArray firstObject] componentsSeparatedByString:@"-"] objectAtIndex:1] integerValue];
            
            year = [[[[dateArray firstObject] componentsSeparatedByString:@"-"] firstObject] integerValue];
            
            currentYear = [[[[currentDateArray firstObject] componentsSeparatedByString:@"-"] firstObject] integerValue];
        }

        if (currentDay - day == 0 && year == currentYear && month == currentMonth) {
           
            if (difference < 60) {
                
                time = @"刚刚";
                
            } else if (difference < 60 * 60) {
                
                NSInteger differentMinute = difference / 60;
                
                time = [NSString stringWithFormat:@"%ld分钟前", (long)differentMinute];
                
            } else {
                
                NSInteger differentHour = difference / 3600;
                
                time = [NSString stringWithFormat:@"%ld小时前", (long)differentHour];
                
            }
           
        } else if (currentDay - day == 1 && year == currentYear && month == currentMonth) {
           
           time = @"昨天";
           
        } else {
                       
            time = [NSString stringWithFormat:@"%ld天前", (long)(difference / 60 / 60 / 24)];
        }

        return time;
        
    } else {
     
        NSLog(@"转换常规时间格式时，字符串不存在");
    }
    
    return @"";

}

@end
